šŸ“” Sparse Table Signal Tower Visualizer

Interactive Range Maximum Query with Sliding Window Analysis

šŸ“‹ Problem Statement

In a high-performance wireless communication grid, there are N signal towers aligned in a straight line. Each tower broadcasts a signal with varying strength, recorded once per day. To evaluate network stability, engineers must frequently analyze segments of the grid to identify the most reliable zones for stable coverage.

Given a range of towers [L, R] and a fixed segment length K, the engineers must determine the minimum of all maximum signal strengths over every K-length subsegment within [L, R].

To support this analysis efficiently, you are required to use a Sparse Table to preprocess the signal strength data for fast Range Maximum Queries.

šŸ“„ Input Format

  • First line: N and K (number of towers and subsegment length)
  • Second line: N space-separated integers (signal strengths, 1 ≤ strength ≤ 100)
  • Third line: Q (number of queries, 1 ≤ Q ≤ 20)
  • Next Q lines: Two integers L and R (0 ≤ L ≤ R āˆ’ K + 1 < N)

šŸ“¤ Output Format

For each query, output a single integer representing the minimum of all K-length segment maximums in the range [L, R].

šŸŽÆ Sample Test Case 1

Input:
8 3
60 72 55 80 65 90 50 78
2
0 4
2 5
Output:
72
80
Explanation:
  • Query [0, 4] with K=3:
    • Segment [0,2]: {60,72,55} → max = 72
    • Segment [1,3]: {72,55,80} → max = 80
    • Segment [2,4]: {55,80,65} → max = 80
    • Result: min(72, 80, 80) = 72
  • Query [2, 5] with K=3:
    • Segment [2,4]: {55,80,65} → max = 80
    • Segment [3,5]: {80,65,90} → max = 90
    • Result: min(80, 90) = 80

šŸŽÆ Sample Test Case 2

Input:
10 4
88 95 70 85 60 92 76 80 69 74
2
1 6
3 9
Output:
92
80

🧠 Sparse Table with Sliding Window

This problem combines Sparse Table for Range Maximum Query (RMQ) with a sliding window technique to find the minimum among all K-length segment maximums.

šŸ“Š Two-Phase Approach

Phase 1: Build Sparse Table - Preprocess the array for O(1) range maximum queries

Phase 2: Sliding Window - For each query [L, R], slide a K-length window and use the sparse table to find the maximum in each window, then return the minimum of these maximums

šŸ”§ Phase 1: Building Sparse Table (for MAX)

// Initialize for length 1 (2^0)
for (int i = 0; i < n; i++) {
    sparse[i][0] = arr[i];
}

// Build for increasing powers of 2
for (int j = 1; (1 << j) <= n; j++) {
    for (int i = 0; i + (1 << j) <= n; i++) {
        sparse[i][j] = max(sparse[i][j-1],
                          sparse[i + (1 << (j-1))][j-1]);
    }
}

sparse[i][j] = maximum value in range starting at index i with length 2^j

šŸ” Phase 2: Query with Sliding Window

// Query maximum in range [L, R] using sparse table
int queryMax(int L, int R) {
    int length = R - L + 1;
    int k = log2(length);
    return max(sparse[L][k],
              sparse[R - (1 << k) + 1][k]);
}

// Main query: Find min of all K-segment maxs in [L, R]
int solve(int L, int R, int K) {
    int minOfMaxs = INT_MAX;

    // Slide window of size K from L to R-K+1
    for (int i = L; i <= R - K + 1; i++) {
        int segmentMax = queryMax(i, i + K - 1);
        minOfMaxs = min(minOfMaxs, segmentMax);
    }

    return minOfMaxs;
}

⚔ Complexity Analysis

Build: O(n log n) Query: O((R-L) Ɨ 1) = O(n) Space: O(n log n)
  • Build: O(n log n) - construct sparse table once
  • Per Query: O(R-L) - slide window and query in O(1) each time
  • Total: O(n log n + Q Ɨ n) - much better than O(Q Ɨ n Ɨ K) naive

šŸ’” Key Insights

  • Sparse Table enables O(1) range maximum queries using overlapping power-of-2 ranges
  • For range [L, R], we can find max using two overlapping ranges of length 2^k where k = floor(log2(R-L+1))
  • The sliding window examines exactly (R - L - K + 2) segments of length K
  • We're looking for the minimum stability point - the K-segment with the smallest peak signal

šŸŽÆ Visual Example

Array: [60, 72, 55, 80, 65], K=3, Query [0, 4]

Window 1 [0,2]: max(60, 72, 55) = 72
Window 2 [1,3]: max(72, 55, 80) = 80
Window 3 [2,4]: max(55, 80, 65) = 80

Result: min(72, 80, 80) = 72 āœ“

šŸ“Š Input Configuration

Normal
Current Segment
Max in Segment
Final Result

šŸ“” Signal Tower Array

šŸ—‚ļø Sparse Table (Range Maximum)

āš™ļø Current Operation

Ready to build table...
Operation log will appear here...